package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.CrriculumDao;
import com.swt.domain.Courses;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Repository
public class CrriculumDaoImpl implements CrriculumDao {
    @Resource
    private HibernateTemplate hibernateTemplate;

    @Override
    public boolean crriculumAdd(String name, String addcalorie, String addGrade, String addType, String addIntro, String imgPWT,int clubClubId) {

      Courses courses  = new Courses();

      courses.setCoursesname(name);
      courses.setCoursescalorie(addcalorie);
      courses.setCoursesgrade(addGrade);
      courses.setCoursestype(addType);
      courses.setCoursesintro(addIntro);
      courses.setCoursesimg(imgPWT);
      courses.setClubClubId(clubClubId);

      try {
          Serializable save = hibernateTemplate.save(courses);
      }
      catch (Exception e){
          return false;
      }

        return true;

    }

    @Override
    public List<Courses> crriculumquery1(int page,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Courses where clubClubId = ?";
        List<Courses> coursesList = session.createQuery(sql).setParameter(0,clubClubId).setFirstResult(page).setMaxResults(9).list();


        return coursesList;
    }

    @Override
    public List<Courses> crriculumquery2(int id,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Courses where id = ? and clubClubId = ?";
        Query query = session.createQuery(sql);
        List<Courses> coursesList1 = query.setParameter(0,id).setParameter(1,clubClubId).list();

        return coursesList1;
    }

    @Override
    public boolean deleteCrriculum(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Courses courses = session.get(Courses.class,id);

        try {
            session.delete(courses);
        }catch (Exception e){
            return false;
        }

        return true;
    }

    @Override
    public boolean updateCrriculum(Courses courses) {

        try {
            hibernateTemplate.update(courses);
        }catch (Exception e){
        return false;
        }



        return true;
    }

//    来自排课页面的查询
    @Override
    public List<Courses> queryCoursesPK(int clubClubId) {

        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Courses where clubClubId = ? ";
        List<Courses> coursesList1 = session.createQuery(sql).setParameter(0,clubClubId).list();
        return coursesList1;

    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
